<template>
    <div>
        <Modal :title="modalTitle" v-model="modalShow" :width="modalWidth" :mask-closable="false" :closable="false">
            <div v-show="modalType === 'edit' || modalType === 'add'">
                <Form ref="formItem" :rules="ruleValidate" label-position="right" :model="formItem" :label-width="130">
                    <FormItem label="姓名：" prop="name">
                        <Input type="text" v-model="formItem.name" placeholder="请输入姓名" clearable></Input>
                    </FormItem>
                    <FormItem label="证件号：" prop="idcard">
                        <Input type="text" v-model="formItem.idcard" placeholder="请输入证件号" clearable></Input>
                    </FormItem>
                </Form>
            </div>
            <div slot="footer">
                <template v-if="modalType === 'edit' || modalType === 'add'">
                    <Button type="text" @click="cancelOption">取消</Button>
                    <Button type="success" @click="saveOption" :loading="btnLoading">保存</Button>
                </template>
                <template v-if="modalType === 'info'">
                    <Button type="text" @click="cancelOption">关闭</Button>
                </template>
            </div>
        </Modal>
    </div>
</template>

<script>
// import { identityCodeValid } from '@/libs/tools'

// const validateCardno = (rule, value, callback) => {
//   const rtn = identityCodeValid(value)
//   if (!rtn.pass) {
//     callback(new Error(rtn.tip))
//   } else {
//     callback()
//   }
// }
import { insertCard } from '@/api/record'
export default {
  name: 'optionModal',
  props: {
    modalTitle: {
      type: String,
      default: () => {
        return ''
      }
    },
    modalType: {
      type: String,
      default: () => {
        return 'info'
      }
    },
    modalShow: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    modalWidth: {
      type: Number,
      default: () => {
        return 800
      }
    },
    dataObj: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      ruleValidate: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        idcard: [
          // { required: true, validator: validateCardno, trigger: 'blur' }
          { required: true, message: '证件号不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ]
      },
      btnLoading: false,
      formItem: {},
      categoryObj: {}
    }
  },
  methods: {
    cancelOption () {
      this.$emit('cancelOption')
    },
    saveOption () {
      let param = {}
      if (this.modalType === 'add') {
        param = this.formItem
        this.$delete(param, 'id')
      } else if (this.modalType === 'edit') {
        param = this.formItem
      }
      this.$refs['formItem'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          insertCard(param).then(ret => {
            let data = ret.data
            if (data.errcode === 0) {
              this.btnLoading = false
              this.success()
              this.$emit('saveOption')
            } else {
              this.btnLoading = false
              this.error(data)
            }
          })
        } else {
          this.$Message.error({
            content: '操作失败,请核对添加信息后重试'
          })
        }
      })
    },
    success (val) {
      val = this.$common.xeUtils.isEmpty(val) ? '操作成功' : val
      this.$Message.success(val)
    },
    error (data) {
      this.$Message.error({
        content: '操作失败,\n' + data.errmsg
      })
    }
  },
  mounted () {
  },
  watch: {
    dataObj (val) {
      this.formItem = val
    }
  },
  computed: {
    typeName () {
      if (this.formItem.type === '1') {
        return '红码'
      } else if (this.formItem.type === '3') {
        return '黄码'
      } else if (this.formItem.type === '4') {
        return '绿码'
      } else {
        return '无'
      }
    }
  }
}
</script>

<style scoped>
</style>
